home *** CD-ROM | disk | FTP | other *** search
/ OK PC 47 - Disc 1 / OK PC 47-1.zip / OK PC 47 - Duke Nukem 3D / OKPC47-1.iso / wing / utils.h_ / utils.h
Text File  |  1994-08-13  |  5KB  |  128 lines

  1. /**************************************************************************
  2.  
  3.     UTILS.H - useful functions for WinG Sample Apps
  4.  
  5.  **************************************************************************/
  6. /***************************************************************************
  7.  
  8.     (C) Copyright 1994 Microsoft Corp.  All rights reserved.
  9.  
  10.     You have a royalty-free right to use, modify, reproduce and 
  11.     distribute the Sample Files (and/or any modified version) in 
  12.     any way you find useful, provided that you agree that 
  13.     Microsoft has no warranty obligations or liability for any 
  14.     Sample Application Files which are modified. 
  15.  
  16.  **************************************************************************/
  17.  
  18. /***************************************************************************
  19.     Functions for handling Device Independent Bitmaps and clearing the 
  20.     System Palette.                                                    
  21.  **************************************************************************/
  22.  
  23. #ifndef SAMPLES_UTILS_H  
  24. #define SAMPLES_UTILS_H
  25.  
  26. #ifdef __cplusplus
  27. extern "C" {
  28. #endif 
  29.  
  30. typedef     LPBITMAPINFOHEADER PDIB;
  31. typedef     HANDLE             HDIB;
  32.  
  33. /***************************************************************************
  34.    External function declarations
  35.  **************************************************************************/
  36.  
  37. void        ClearSystemPalette(void);
  38. PDIB        DibOpenFile(LPSTR szFile);
  39. BOOL        DibSetUsage(PDIB pdib, HPALETTE hpal,UINT wUsage);
  40. PDIB        DibCreate(int bits, int dx, int dy);
  41. void        DibMapToPalette(PDIB pdib, HPALETTE hpal);  
  42.  
  43. /****************************************************************************
  44.    Internal function declarations
  45.  ***************************************************************************/
  46.  
  47. PDIB        DibReadBitmapInfo(HFILE fh);
  48.  
  49. /****************************************************************************
  50.    DIB macros.
  51.  ***************************************************************************/
  52.  
  53. #ifdef  WIN32
  54.     #define HandleFromDib(lpbi) GlobalHandle(lpbi)
  55. #else
  56.     #define HandleFromDib(lpbi) (HANDLE)GlobalHandle(SELECTOROF(lpbi))
  57. #endif
  58.  
  59. #define DibFromHandle(h)        (PDIB)GlobalLock(h)
  60.  
  61. #define DibFree(pdib)           GlobalFreePtr(pdib)
  62.  
  63. #define WIDTHBYTES(i)           ((unsigned)((i+31)&(~31))/8)  /* ULONG aligned ! */
  64.  
  65. #define DibWidth(lpbi)          (UINT)(((LPBITMAPINFOHEADER)(lpbi))->biWidth)
  66. #define DibHeight(lpbi)         (UINT)(((LPBITMAPINFOHEADER)(lpbi))->biHeight)
  67. #define DibBitCount(lpbi)       (UINT)(((LPBITMAPINFOHEADER)(lpbi))->biBitCount)
  68. #define DibCompression(lpbi)    (DWORD)(((LPBITMAPINFOHEADER)(lpbi))->biCompression)
  69.  
  70. #define DibWidthBytesN(lpbi, n) (UINT)WIDTHBYTES((UINT)(lpbi)->biWidth * (UINT)(n))
  71. #define DibWidthBytes(lpbi)     DibWidthBytesN(lpbi, (lpbi)->biBitCount)
  72.  
  73. #define DibSizeImage(lpbi)      ((lpbi)->biSizeImage == 0 \
  74.                                     ? ((DWORD)(UINT)DibWidthBytes(lpbi) * (DWORD)(UINT)(lpbi)->biHeight) \
  75.                                     : (lpbi)->biSizeImage)
  76.  
  77. #define DibSize(lpbi)           ((lpbi)->biSize + (lpbi)->biSizeImage + (int)(lpbi)->biClrUsed * sizeof(RGBQUAD))
  78. #define DibPaletteSize(lpbi)    (DibNumColors(lpbi) * sizeof(RGBQUAD))
  79.  
  80. #define DibFlipY(lpbi, y)       ((int)(lpbi)->biHeight-1-(y))
  81.  
  82. //HACK for NT BI_BITFIELDS DIBs
  83. #ifdef WIN32
  84.     #define DibPtr(lpbi)            ((lpbi)->biCompression == BI_BITFIELDS \
  85.                                        ? (LPVOID)(DibColors(lpbi) + 3) \
  86.                                        : (LPVOID)(DibColors(lpbi) + (UINT)(lpbi)->biClrUsed))
  87. #else
  88.     #define DibPtr(lpbi)            (LPVOID)(DibColors(lpbi) + (UINT)(lpbi)->biClrUsed)
  89. #endif
  90.  
  91. #define DibColors(lpbi)         ((RGBQUAD FAR *)((LPBYTE)(lpbi) + (int)(lpbi)->biSize))
  92.  
  93. #define DibNumColors(lpbi)      ((lpbi)->biClrUsed == 0 && (lpbi)->biBitCount <= 8 \
  94.                                     ? (int)(1 << (int)(lpbi)->biBitCount)          \
  95.                                     : (int)(lpbi)->biClrUsed)
  96.  
  97. #define DibXYN(lpbi,pb,x,y,n)   (LPVOID)(                                     \
  98.                                 (BYTE _huge *)(pb) +                          \
  99.                                 (UINT)((UINT)(x) * (UINT)(n) / 8u) +          \
  100.                                 ((DWORD)DibWidthBytesN(lpbi,n) * (DWORD)(UINT)(y)))
  101.  
  102. #define DibXY(lpbi,x,y)         DibXYN(lpbi,DibPtr(lpbi),x,y,(lpbi)->biBitCount)
  103.  
  104. #define FixBitmapInfo(lpbi)     if ((lpbi)->biSizeImage == 0)                 \
  105.                                     (lpbi)->biSizeImage = DibSizeImage(lpbi); \
  106.                                 if ((lpbi)->biClrUsed == 0)                   \
  107.                                     (lpbi)->biClrUsed = DibNumColors(lpbi);   \
  108.                                 if ((lpbi)->biCompression == BI_BITFIELDS && (lpbi)->biClrUsed == 0) \
  109.                                     ; // (lpbi)->biClrUsed = 3;                    
  110.  
  111. #define DibInfo(pDIB)     ((BITMAPINFO FAR *)(pDIB))
  112.  
  113. /***************************************************************************/
  114.  
  115. #ifndef BI_BITFIELDS
  116.   #define BI_BITFIELDS 3
  117. #endif  
  118.  
  119. #ifndef HALFTONE
  120.   #define HALFTONE COLORONCOLOR
  121. #endif
  122.  
  123. #ifdef __cplusplus
  124. }
  125. #endif 
  126.  
  127. #endif
  128.